package com.gopay.common.cps.manager.gsrt.impl;

import com.gopay.common.cps.dao.gsrt.GsrtFailureBidsSubDao;
import com.gopay.common.cps.manager.gsrt.GsrtFailureBidsSubQueryManager;
import com.gopay.common.domain.gsrt.GsrtFailureBidsSub;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.common.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("gsrtFailureBidsSubQueryManager")
public class GsrtFailureBidsSubQueryManagerImpl implements GsrtFailureBidsSubQueryManager {

	@Resource(name = "gsrtFailureBidsSubDao")
	private GsrtFailureBidsSubDao gsrtFailureBidsSubDao;
	@Resource(name = "commonQueryDAO")
	private CommonQueryDAO commonQueryDAO;
	
	@Override
	public List<GsrtFailureBidsSub> getByGopayId(String gopayOrderId) {
		String hql=" from GsrtFailureBidsSub where gopayOrderId=:gopayOrderId ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("gopayOrderId", gopayOrderId);
		List<GsrtFailureBidsSub> list=gsrtFailureBidsSubDao.findByHql(hql, param);
		return list;
	}
	@Override
	public List<GsrtFailureBidsSub> getByColumn(String columnName, String columnValue) {
		String hql=" from gsrtFailureBidsSub where " + 
					columnName + " =:columnValue ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put(columnName, columnValue);
		List<GsrtFailureBidsSub> list=gsrtFailureBidsSubDao.findByHql(hql, param);
		return list;
	}
	@Override
	public List<GsrtFailureBidsSub> getByMerIdOrderId(String merId, String orderId) {
		// TODO Auto-generated method stub
		return gsrtFailureBidsSubDao.getByMerIdOrderId(merId, orderId);
	}
	@Override
	public List<BigDecimal> getSumAmtByMasterIdConstracter(String masterId, String constracterNo, String invsetMerOrderId) {
		// TODO Auto-generated method stub
		List<BigDecimal> sum = new ArrayList<BigDecimal>();
		StringBuffer sql = new StringBuffer();
		sql.append("select sum(sub.mer_txn_amt), sum(sub.terrace_pay_amt)" +
				" from gsrt_failure_bids_sub sub where sub.txn_sta_cd='20000' ");
		if(!StringUtils.isBlank(masterId)){
			sql.append(" and sub.FAILURE_BIDS_MASTER_ID = '" + masterId + "' ");
		}
		if(!StringUtils.isBlank(constracterNo)) {
			sql.append(" and sub.CONSTRACTER_ID = '" + constracterNo + "' ");
		}
		if(!StringUtils.isBlank(invsetMerOrderId)) {
			sql.append(" and sub.INVEST_ORDER_ID = '" + invsetMerOrderId + "' ");
		}
		List<Object[]> results = commonQueryDAO.findBySql(sql.toString());
		if(results.size() > 0)
		{
			Object[] result = results.get(0);
			sum.add(new BigDecimal(result[0] == null? "0.00" : result[0].toString()).setScale(2));
			sum.add(new BigDecimal(result[1] == null? "0.00" : result[1].toString()).setScale(2));
		}
		return sum;
	}
	@Override
	public List<GsrtFailureBidsSub> getByMasterId(String masterId){
		String hql=" from GsrtFailureBidsSub where failureBidsMasterId=:failureBidsMasterId ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("failureBidsMasterId", masterId);
		List<GsrtFailureBidsSub> list=gsrtFailureBidsSubDao.findByHql(hql, param);
		return list;
	}
	@Override
    public List<GsrtFailureBidsSub> getByMerOrderId(String mercustId, String merorderId) {
		String sql=" select sub.txn_sta_cd,sub.comment_rk ,masters.mer_cust_id,sub.mer_order_id   "
				+ ",sub.gopay_order_id,sub.gopay_txn_tm from gsrt_failure_bids_master  masters left  join   gsrt_failure_bids_sub "
				+ " sub  on masters.id=sub.failure_bids_master_id   "
				+ "where masters.mer_cust_id='"+mercustId+"' "
				+ "and sub.mer_order_id='"+merorderId+"'";
			List<Object[]> list= commonQueryDAO.findBySql(sql);
			List<GsrtFailureBidsSub>  listsub=new ArrayList<GsrtFailureBidsSub>();
			GsrtFailureBidsSub sub=null;
			for (Object[] objects : list) {
				sub=new GsrtFailureBidsSub();
				if(objects[0]!=null){
					BigDecimal  bg=new BigDecimal(objects[0].toString());
					sub.setTxnStaCd(bg.intValue());;
				}
				if(objects[1]!=null){
					sub.setCommentRk(objects[1].toString());
				}
				if(objects[4]!=null){
					sub.setGopayOrderId(objects[4].toString());
				}
				if(objects[5]!=null){
					sub.setGopayTxnTm(DateUtils.strToDate(objects[5].toString()));
				}
				
				listsub.add(sub);
			}
			return listsub;
		}
}
